import { Company, convertCompany } from "@shared/types";
import { get, post } from "@ugeez/frontend-commons/lib/service";
import { SearchResult, SourceData } from "@ugeez/frontend-commons/lib/types";
import { convertSearchResult } from "@ugeez/frontend-commons/lib/types/converter";

export const query = async (id: string): Promise<Company> => {
    const res: SourceData = await get('/company/query', {
        id
    });

    const obj: Company = convertCompany(res);
    return obj;
};

export const list = async (page: number, pageSize: number): Promise<SearchResult<Company>> => {
    const res: SourceData = await get('/company/list', {
        page, 
        pageSize
    });

    return convertSearchResult<Company>(res, (obj) => {
        const item: Company = convertCompany(obj);
        return item;
    });
};

export const create = async (company: Company): Promise<string> => {
    const { name, address, city, province, registryTime, type, employeeNum, biz } = company;
    const res: SourceData = await post('/company/create', {
        name, 
        address, 
        city, 
        province, 
        registryTime, 
        type,
        employeeNum, 
        biz
    });

    return res.id;
};

export const update = async (company: Company): Promise<void> => {
    const { id, name, address, city, province, registryTime, type, employeeNum, biz } = company;
    await post('/company/update', {
        id, 
        name, 
        address, 
        city, 
        province, 
        registryTime, 
        employeeNum, 
        type, 
        biz
    });
};

export const remove = async (id: string): Promise<void> => {
    await post('/company/delete', {
        id
    });
};